<?php
/**
 * 
 * @authors Liujinbi (857053791@QQ.com)
 * @date    2017-09-26 11:06:27
 * @version $Id$
 */

namespace app\admin\controller;
use think\Db;
use think\Request;
class Returnstock extends Base{
    
    function index(){
        $keywords = Request()->get('keywords');
        $sort = Request()->get('create_time')?Request()->get('create_time'):"create_time desc";
        $this->assign('sort',$sort);
        $Query = Db::field('sr.*,Date(sr.create_time) as createtime,sum(gr.rg_num) as return_num,o.receiver_name,receiver_mobile,s.shop_name,o.agent_name')
        ->table('sdzx_shipping_refund')->alias('sr')
        ->join('sdzx_order_goods_return gr','sr.order_id=gr.order_id and gr.rg_status=0')
        ->join('sdzx_order o','sr.order_id=o.order_id')
        ->join('sdzx_shop s','o.user_id=s.user_id','left')
        ->having("min(gr.rg_status)=0")
        ->group('sr.order_id')
        ->order('sr.'.$sort);
        
        if ($keywords) {
            $Query->where("delivery_name like '%{$keywords}%' or return_num like '%{$keywords}%' ");
        }
        $list = $Query->select();
        return $this->fetch('',['list'=>$list]);
    }

    public function entstock()
    {
    	if (Request()->isPost()) {
    		$param = Request()->param();
    		$time = $param['time'];
    		$data = $param['data'];
    		Db::startTrans();
    		foreach ($data as $k => $v) {
    			$old = Db::name('goods_specval')->where(['specval_id'=>$v['specval_id']])->find();
				if (empty($old)) {continue;}

    			$up = [ 'entstock_num' => ['exp',"entstock_num+{$v['num']}"]];

    			if ($v['rg_num']==$v['num']) {
    				$up['rg_status'] = 1;
    			}
    			$re_gr = Db::name('order_goods_return')->where(['rg_id'=>$v['rg_id']])->update($up);
    			if ($re_gr===false) {
    				Db::rollback();
    				$this->error('入库失败');
    			}

    			$stock_id = Db::connect([],'pg')->name('stock')->where(['specval_id' => $v['specval_id']])->value('stock_id');
    			if (!$stock_id) {
    				Db::rollback();
    				$this->error('入库失败');
    			}

    			// 更新需要采购数量
    			$pre_stock = Db::name('goods_specval')->where(['specval_id'=>$v['specval_id']])->setInc('stock',$v['num']);
    			if ($re_stock===false) {
    				Db::rollback();
    				$this->error('入库失败');
    			}

    			// 更新库存
    			$re_stock = Db::name('stock')->where(['specval_id'=>$v['specval_id']])->setInc('stock_num',$v['num']);
    			if ($re_stock===false) {
    				Db::rollback();
    				$this->error('入库失败');
    			}
    			//库存详情
				// -10：退货出库
				// -1  ：配送出库
				// 1  ：采购入库
				// 10：退单入库
				Db::connect([],'pg')->name('stock_detail')->insert([
					'stock_id'     => $stock_id,
					'goods_id'     => $old['goods_id'],
					'specval_name' => $old['specval_name'],
					'specval_id'   => $old['specval_id'],
					'type'         => 10,
					'num'          => $v['num'],
					'enter_price'  => $v['enter_price'],
					'time'         => date('Y-m-d H:i:s'),
					'uid'          => $this->uid,
					'uname'        => $this->user['name'],
				]);

				//更新状态
				$re_gr_status = Db::name('order_goods_return')->where(['rg_id'=>$v['rg_id']])->find();
				if ($re_gr_status['rg_num'] <= $re_gr_status['entstock_num']) {
					Db::name('order_goods_return')->where(['rg_id'=>$v['rg_id']])->update(['rg_status'=>1]);
				}
    		}
    		Db::commit();
    		$this->success('入库成功','refresh');
    	}else{
    		// $delivery_id = Request()->param('delivery_id');
	    	// $time = Request()->param('time');
	    	// $list = Db::field('sr.*,gr.*,sum(gr.rg_num) as rgnum, og.goods_picture,og.price,og.goods_specval_name as specval_name,og.brand_name')
	     //    ->table('sdzx_shipping_refund')->alias('sr')
	     //    ->join('sdzx_order_goods_return gr','sr.order_id=gr.order_id and gr.rg_status=0')
	     //    ->join('sdzx_order_goods og','gr.og_id=og.order_goods_id')
	     //    ->where("sr.status=0 and sr.delivery_id={$delivery_id} and Date(sr.create_time) = '{$time}'")
	     //    ->order('sr.create_time asc')
	     //    ->group('gr.specval_id')
	     //    ->select();
	     //    return $this->fetch('',['list'=>$list,'time'=>"'{$time}'"]);
            $delivery_id = Request()->param('delivery_id');
            $order_id = Request()->param('order_id');
            $list = Db::field('sr.*,gr.*,sum(gr.rg_num) as rgnum, og.goods_picture,og.price,og.goods_specval_name as specval_name,og.brand_name')
            ->table('sdzx_shipping_refund')->alias('sr')
            ->join('sdzx_order_goods_return gr','sr.order_id=gr.order_id and gr.rg_status=0')
            ->join('sdzx_order_goods og','gr.og_id=og.order_goods_id')
            ->where("sr.status=0 and sr.delivery_id={$delivery_id} and sr.order_id = '{$order_id}'")
            ->order('sr.create_time asc')
            ->group('gr.specval_id')
            ->select();

            $order = Db::field('o.*,s.shop_name')
            ->table('sdzx_order')->alias('o')
            ->join('sdzx_shop s','o.user_id=s.user_id','left')
            ->where(['order_id'=>$order_id])
            ->find();
            return $this->fetch('',['list'=>$list,'order'=>$order]);   
    	}
    }
}